package com.mms.utsc.smartnet;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class AddCourseActivity extends Activity implements OnClickListener {
	Button addBtn;
	Button clearBtn;
	Button genBtn;
	EditText course;
	EditText day;
	EditText start;
	EditText end;
	int count =0;
	Spinner editcourse;
	ArrayList<Schedule> schedule = new ArrayList<Schedule>();
	DateFormat formatter = new SimpleDateFormat("hh:mm");
	
	@Override
	protected void onCreate(Bundle savedInstatnceState) {
		super.onCreate(savedInstatnceState);
		setContentView(R.layout.add_courses);
		
		addBtn = (Button) findViewById(R.id.bAdd);
		clearBtn = (Button) findViewById(R.id.bClear);
		genBtn = (Button) findViewById(R.id.bGen);
		course = (EditText) findViewById(R.id.etCourse);
		day = (EditText) findViewById(R.id.etDay);
		start = (EditText) findViewById(R.id.etStart);
		end = (EditText) findViewById(R.id.etEnd);
		addBtn.setOnClickListener(this);
		clearBtn.setOnClickListener(this);
		genBtn.setOnClickListener(this);		
		editcourse = (Spinner) findViewById(R.id.editcourse);
		ArrayAdapter<Schedule> adapter=new ArrayAdapter<Schedule>  
        (this,android.R.layout.simple_spinner_dropdown_item,schedule);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        editcourse.setAdapter(adapter);
        addListenerOnSpinnerItemSelection();
	}
	
	public void onClick(View view) {		
		switch (view.getId()) {
		case R.id.bAdd:
			if (addBtn.getText().equals("Add")) {
				addCourse();
			}
			else {
				saveCourse();
				addBtn.setText("Add"); //because add and save button is using the same id, so after save need to change text to "add"
			}
				
			break;
		case R.id.bClear:
			course.setText(null);
			day.setText(null);
			start.setText(null);
			end.setText(null);
			break;
		case R.id.bGen:
			//go to generate activity
			VarHolder.schedule = schedule;
			Intent displayIntent = new Intent(this, TimetableActivity.class);
			startActivity(displayIntent);
			count = 0;
		}
		
	}
	
	public void addListenerOnSpinnerItemSelection() {
		editcourse = (Spinner) findViewById(R.id.editcourse);
		editcourse.setOnItemSelectedListener(new CustomOnItemSelectedListener());
	}
	
	public class CustomOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Schedule toBeEdited = (Schedule) editcourse.getItemAtPosition(arg2); 
			course.setText(toBeEdited.getCourse());
			day.setText(toBeEdited.getDay());
			start.setText(toBeEdited.getStartTime().getHours()+""+":00");
			end.setText(toBeEdited.getEndTime().getHours()+""+":00");
			addBtn.setText("Save");
			
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	private void addCourse(){
		Date _start = null;
		Date _end = null;
		String c = course.getText().toString();
		String d = day.getText().toString();
		try {
			_start = formatter.parse(start.getText().toString());
			_end = formatter.parse(end.getText().toString());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		schedule.add(new Schedule(c,d,_start,_end));
		course.setText(null);
		day.setText(null);
		start.setText(null);
		end.setText(null);
	}
	
	private void saveCourse(){
		//TODO must save course to db instead of a new entry 
	}

}
